using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco.Graphics;
using WPCoco;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FluidDemo.Scenes
{
    public class AlphaTestNode : TextureNode
    {
        public AlphaTestNode(AffineNode node, bool autoSize)
            : base(node, autoSize)
        {
            effect = new AlphaTestEffect(Screen.CurrentGraphicsDevice);
            effect.ReferenceAlpha = 128;
            effect.AlphaFunction = CompareFunction.Greater;
            effect.Alpha = 1;
            effect.Projection = Matrix.CreateOrthographicOffCenter(1, Screen.Width,     // left, right
                                                                   Screen.Height, 1,    // bottom, top
                                                                   0, 1);
            effect.World = Matrix.Identity;
        }

        float referenceAlpha = 0.5f;

        public float ReferenceAlpha
        {
            get { return referenceAlpha; }
            set 
            { 
                referenceAlpha = value;
                effect.ReferenceAlpha = (int)(value * 255);
            }
        }

        public float AlphaAddition { get { return effect.Alpha; } set { effect.Alpha = value; } }

        public CompareFunction Function { get { return effect.AlphaFunction; } set { effect.AlphaFunction = value; } }

        AlphaTestEffect effect;
        VertexPositionColorTexture[] quads = new VertexPositionColorTexture[6];

        public override void DrawTexture(Texture2D texture, Size size)
        {
            effect.Texture = texture;
            effect.World = WorldMatrix.Shared.CurrentMatrix;

            Rectangle rect = new Rectangle(0, 0, size.Width, size.Height);

            CachedSpriteBatch spriteBatch = SpriteBatchCache.GetVolatile();
            spriteBatch.Begin(SpriteSortMode.Deferred, Blend, Sampler, DepthStencil, Rasterizer, effect, WorldMatrix.Shared.CurrentMatrix);
            spriteBatch.Draw(texture, rect, rect, Color);
            spriteBatch.End();
        }

    }
}
